package com.examstack.portal.controller.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.examstack.common.Constants;
import com.examstack.common.domain.exam.Exam;
import com.examstack.common.domain.exam.Message;
import com.examstack.common.domain.exam.UserQuestionHistory;
import com.examstack.common.domain.question.QuestionHistory;
import com.examstack.portal.persistence.ExamMapper;
import com.examstack.portal.persistence.ExamPaperMapper;
import com.examstack.portal.security.UserInfo;
import com.examstack.portal.service.QuestionHistoryService;

@Controller
public class PracticeAction {
	@Autowired
	private QuestionHistoryService questionHistoryService;
	@Autowired
	private ExamPaperMapper examPaperMapper;
	@Autowired
	private ExamMapper examMapper;
	/**
	 * 练习模式完成一道题
	 * 
	 * @param sp
	 * @return
	 */
	@RequestMapping(value = "/student/practice-improve", method = RequestMethod.POST)
	public @ResponseBody Message submitPractice(@RequestBody QuestionHistory qh) {
		Message msg = new Message();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		Integer  count = 0;
		
		List<UserQuestionHistory> historyMap = questionHistoryService.getUserQuestionHistory(userInfo.getUserid(), qh.getQuestionId());
	
		UserQuestionHistory s = historyMap.size()>0 ? historyMap.get(0) : null;
		
		UserQuestionHistory history = new UserQuestionHistory();
		history.setQuestionId(qh.getQuestionId());
		history.setUserId(userInfo.getUserid());
		history.setQuestionTypeId(qh.getQuestionTypeId());
		
		boolean isRight = qh.getAnswer().equals(qh.getMyAnswer()) ? true : false;
		
		if(isRight){
			//是否做 对,满足出库限制  过这题 
			if(s != null && !s.isRight() && s.getRightCount() > 0){
				isRight = false;
				count = s.getRightCount()-1;
			}else{
				isRight = true;
			}
		}else{
			count = Constants.RIGHT_COUNT;
		}
		
		if(count == 0){
			isRight = true;
		}
		history.setRight(isRight);
		history.setRightCount(count);
		
		try {
			questionHistoryService.addUserQuestionHist(history);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			msg.setResult(e.getClass().getName());
			e.printStackTrace();
		}

		return msg;
	}
	
	/**
	 * 练习模式完成一次练习
	 * 
	 * @param sp
	 * @return
	 */
	@RequestMapping(value = "/student/practice-improves", method = RequestMethod.POST)
	public @ResponseBody Message submitPractices(@RequestBody List<QuestionHistory> list) {
		Message msg = new Message();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		
		for(QuestionHistory qh : list) {
			Integer  count = 0;
			List<UserQuestionHistory> historyMap = questionHistoryService.getUserQuestionHistory(userInfo.getUserid(), qh.getQuestionId());
			UserQuestionHistory s = historyMap.size()>0 ? historyMap.get(0) : null;
			UserQuestionHistory history = new UserQuestionHistory();
			history.setQuestionId(qh.getQuestionId());
			history.setUserId(userInfo.getUserid());
			history.setQuestionTypeId(qh.getQuestionTypeId());
			boolean isRight = qh.getAnswer().equals(qh.getMyAnswer()) ? true : false;
			if(isRight){
				//是否做 对,满足出库限制  过这题 
				if(s != null && !s.isRight() && s.getRightCount() > 0){
					isRight = false;
					count = s.getRightCount()-1;
				}else{
					isRight = true;
				}
			}else{
				count = Constants.RIGHT_COUNT;
			}
			
			if(count == 0){
				isRight = true;
			}
			history.setRight(isRight);
			history.setRightCount(count);
			
			try {
				questionHistoryService.addUserQuestionHist(history);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				msg.setResult(e.getClass().getName());
				e.printStackTrace();
			}
		}
		
		return msg;
	}
	/***
	 * 移出错题库
	 * 更新用户做题历史
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/student/practice-improve-del", method = RequestMethod.POST)
	public @ResponseBody Message delPracticesErr(@RequestBody QuestionHistory qh) {
		Message msg = new Message();
		UserInfo userInfo = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<UserQuestionHistory> historyMap = questionHistoryService.getUserQuestionHistory(userInfo.getUserid(), qh.getQuestionId());
		
		UserQuestionHistory history = historyMap.size()>0 ? historyMap.get(0) : null;
		if(history != null) {
			history.setQuestionId(qh.getQuestionId());
			history.setUserId(userInfo.getUserid());
			history.setQuestionTypeId(qh.getQuestionTypeId());
			history.setRight(true);
			history.setRightCount(0);
			try {
				questionHistoryService.addUserQuestionHist(history);
			} catch (Exception e) {
				msg.setResult(e.getClass().getName());
				e.printStackTrace();
			}
		}else{
			msg.setResult("查无此题！！");
		}
		return msg;
	}
	
	public static void main(String[] args) {
		int num = 2147483647;
		num+=2L;
		System.out.println(num);
		
		char c='A';
		int num2 = 10;
		switch (c) {
		case 'B': num2++;//break;
		case 'A': num2++;//break;
		case 'Y': num2++;break;
		default : num2--;
		}
		System.out.println(num2);
		int num3 = 68;
		System.out.println((char)num3);
		int num4 = 50;
		num4 = num4++*2;
		System.out.println(num4);
		
		int i = 1;
		int j = i++;
		int k = ++i;
		/*if((i==(++j))&&
				((i++)==j)) {
			i+=j;
		}*/
		System.out.println("i"+i);
		System.out.println("j"+j);
		System.out.println("k"+k);
		System.out.println(Math.round(-15.61));
		
	}
	
	@RequestMapping(value = "/student/practice-exam-del-{examId}", method = RequestMethod.GET)
	public @ResponseBody Message delPracticesExam(@PathVariable("examId") int examId) {
		Message msg = new Message();
		
		try {
			Exam exm= examMapper.getExamById(examId);
			examMapper.deleteExamById(examId);
			examPaperMapper.deleteExamPaper(exm.getExamPaperId());
		} catch (Exception e) {
			msg.setResult(e.getClass().getName());
			e.printStackTrace();
		}
		
		return msg;
	}
}
